Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

279
Vistas
Javascript, replace "," with "." in JSON

I have as an input an invalid JSON string with this structure:

{
 "Seq_N":66,
 "Uptime":728,
 "Hum":33,500000,
 "Temp (C)":20,129999
}

I have no accesso to the code that produces the invalid JSON string so I can't fix the string before it's generated

In order to make the JSON string valid, I need to replace the comma character inside the Hum and Temp fields with a dot character, but I also need to keep the comma separators of the JSON string (in order not to break the JSON syntax)

Does anyone know a clean function/regex to do this?

Thanks in advance

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

You can use regex like this:

var json = `{
 "Seq_N":66,
 "Uptime":728,
 "Hum":33,500000,
 "Temp (C)":20,129999
}`;

json = json.replace(/("(Hum|Temp \(C\))":\d+),/g, '$1.');

console.log(json)
about 4 years ago · Santiago Gelvez Denunciar

0

Negative lookahead would do the job.

const foo = '{ "Seq_N":66, "Uptime":728, "Hum":33,500000, "Temp (C)":20,129999 }';

const cleanedFoo = foo.replace(/,(?![" ])/g, ".");

console.log(JSON.parse(cleanedFoo));

The regex means:

, match comma,

(?! only if it's not followed

[" ] by either double quotes or space. (Yes, this so-called JSON is seriously malformed).

about 4 years ago · Santiago Gelvez Denunciar

0

If I understand the question correctly, the following code should do the job:

$(".stat-item").each(function () {
var inner_context = $(this).text();
$(this).text(inner_context.replace(/[\.]+/g, ","));
});
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda